package com.leyou.item.controller;

import com.leyou.common.dto.PageDTO;
import com.leyou.item.dto.BrandDTO;
import com.leyou.item.entitry.Brand;
import com.leyou.item.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author 张伟奇
 * @Date 2021/7/15 14:05
 * @description
 * @Version 1.0
 **/
@RestController
@RequestMapping("/brand")
public class BrandController {
    @Autowired
    private BrandService brandService;

    /**
     * 根据品牌id查询
     * @param id
     * @return
     */

    @GetMapping("/{id}")
    public ResponseEntity<List<BrandDTO>>  findBrandById(@PathVariable("id") Long id){

        return ResponseEntity.ok(brandService.findBrandById(id));
    }

    /**
     * 根据分类id查询品牌
     * @param id
     * @return
     */

    @GetMapping("/of/category")
    public ResponseEntity<List<BrandDTO>>  findBrandByCategoryId(@RequestParam("id") Long id){

        return ResponseEntity.ok(brandService.findBrandByCategoryId(id));
    }

    /**
     * 根据品牌id集合查询品牌集合
     * @param ids
     * @return
     */
    @GetMapping("/list")
    public ResponseEntity<List<BrandDTO>> findBrandIds(@RequestParam("ids") List<Integer> ids){
        return ResponseEntity.ok(brandService.findBrandIds(ids));
    }

    /**
     * 分页查询品牌
     * @param page
     * @param rows
     * @param key
     * @return
     */
    @GetMapping("/page")
    public ResponseEntity<PageDTO<BrandDTO>> findBrandByPage(
            @RequestParam(value = "page",defaultValue = "1") Integer page,
            @RequestParam(value = "rows",defaultValue = "5") Integer rows,
            @RequestParam(value = "key",required = false) String key){

        return ResponseEntity.ok(brandService.findBrandByPage(page, rows, key));

    }

    /**
     * 添加品牌
     * @param brand
     * @param categoryIds
     * @return
     */

   @PostMapping
   public ResponseEntity<Void> saveBrand(Brand brand, @RequestParam("categoryIds") List<Long> categoryIds){
       brandService.saveBrand(brand,categoryIds);
        return ResponseEntity.status(HttpStatus.CREATED).build();
   }

    /**
     * 修改品牌
     * @param brand
     * @param categoryIds
     * @return
     */
    @PutMapping
    public ResponseEntity<Void> updateBrand(Brand brand, @RequestParam("categoryIds") List<Long> categoryIds){
        brandService.updateBrand(brand,categoryIds);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    /**
     *  根据Id查询
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public ResponseEntity<Void>  deleteBrandById(@PathVariable("id") Long id){

        brandService.deleteBrandId(id);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

}
